{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fp = open('Ydisp.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{d}\\\\mathrm{isp1}_{1} - \\\\mathrm{L1}_{s}\\\\mathrm{n1} + \\\\mathrm{Ci}_{d}\\\\mathrm{isp2}_{1}\\\\, F_{d}\\\\mathrm{isp1}_{2} & 0 & \\\\mathrm{Ci}_{d}\\\\mathrm{isp2}_{3}\\\\, F_{d}\\\\mathrm{isp1}_{2}\\\\\\\\ 0 & H_{d}\\\\mathrm{isp2}_{2} - \\\\mathrm{L1}_{s}0 & 0\\\\\\\\ \\\\mathrm{Ci}_{d}\\\\mathrm{isp2}_{1}\\\\, F_{d}\\\\mathrm{isp3}_{2} & 0 & H_{d}\\\\mathrm{isp3}_{3} - \\\\mathrm{L1}_{s}1 + \\\\mathrm{Ci}_{d}\\\\mathrm{isp2}_{3}\\\\, F_{d}\\\\mathrm{isp3}_{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\mathrm{Kf}_{s}\\\\mathrm{n1} - S_{d}\\\\mathrm{isp1}_{1} - \\\\mathrm{Cv}_{d}\\\\mathrm{isp2}_{1}\\\\, F_{d}\\\\mathrm{isp1}_{2} & 0 &  - S_{d}\\\\mathrm{isp1}_{3} - \\\\mathrm{Cv}_{d}\\\\mathrm{isp2}_{3}\\\\, F_{d}\\\\mathrm{isp1}_{2}\\\\\\\\ 0 & \\\\mathrm{Kf}_{s}0 & 0\\\\\\\\  - S_{d}\\\\mathrm{isp3}_{1} - \\\\mathrm{Cv}_{d}\\\\mathrm{isp2}_{1}\\\\, F_{d}\\\\mathrm{isp3}_{2} & 0 & \\\\mathrm{Kf}_{s}1 - S_{d}\\\\mathrm{isp3}_{3} - \\\\mathrm{Cv}_{d}\\\\mathrm{isp2}_{3}\\\\, F_{d}\\\\mathrm{isp3}_{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & \\\\mathrm{Km}_{s}\\\\mathrm{n1}\\\\, \\\\left(\\\\frac{\\\\overline{\\\\mathrm{M1a}}}{2} + \\\\frac{\\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{Hv}_{s}0\\\\, \\\\mathrm{Vdc0}}{2}\\\\right) & 0\\\\\\\\ \\\\mathrm{Km}_{s}0\\\\, \\\\left(\\\\frac{\\\\mathrm{M1a}}{2} + \\\\frac{\\\\mathrm{Hi}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Hv}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Vdc0}}{2}\\\\right) & 0 & \\\\mathrm{Km}_{s}0\\\\, \\\\left(\\\\frac{\\\\overline{\\\\mathrm{M1a}}}{2} + \\\\frac{\\\\mathrm{Hi}_{s}1\\\\, \\\\mathrm{Hv}_{s}1\\\\, \\\\mathrm{Vdc0}}{2}\\\\right)\\\\\\\\ 0 & \\\\mathrm{Km}_{s}1\\\\, \\\\left(\\\\frac{\\\\mathrm{M1a}}{2} + \\\\frac{\\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{Hv}_{s}0\\\\, \\\\mathrm{Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, \\\\mathrm{I1a}\\\\, \\\\mathrm{Kf}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1}}{4\\\\, \\\\mathrm{Vdc0} - 2\\\\, \\\\mathrm{Iload}\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1} + 2\\\\, \\\\mathrm{Cdc}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Vdc0}\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1}} & 0 & -\\\\frac{3\\\\, \\\\mathrm{Kf}_{s}1\\\\, \\\\mathrm{Zdcn}_{s}1\\\\, \\\\overline{\\\\mathrm{I1a}}}{4\\\\, \\\\mathrm{Vdc0} - 2\\\\, \\\\mathrm{Iload}\\\\, \\\\mathrm{Zdcn}_{s}1 + 2\\\\, \\\\mathrm{Cdc}_{s}1\\\\, \\\\mathrm{Vdc0}\\\\, \\\\mathrm{Zdcn}_{s}1}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1}\\\\, \\\\left(\\\\mathrm{I1a}\\\\, \\\\mathrm{L1}_{s}\\\\mathrm{n1} + \\\\mathrm{I1a}\\\\, \\\\mathrm{L1st} + \\\\mathrm{Kfst}\\\\, \\\\mathrm{U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, \\\\mathrm{Vdc0} - \\\\mathrm{Iload}\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1} + \\\\mathrm{Cdc}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Vdc0}\\\\, \\\\mathrm{Zdcn}_{s}\\\\mathrm{n1}\\\\right)} & 0 & -\\\\frac{3\\\\, \\\\mathrm{Zdcn}_{s}1\\\\, \\\\left(\\\\mathrm{L1}_{s}1\\\\, \\\\overline{\\\\mathrm{I1a}} + \\\\mathrm{L1st}\\\\, \\\\overline{\\\\mathrm{I1a}} + \\\\mathrm{Kfst}\\\\, \\\\overline{\\\\mathrm{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, \\\\mathrm{Vdc0} - \\\\mathrm{Iload}\\\\, \\\\mathrm{Zdcn}_{s}1 + \\\\mathrm{Cdc}_{s}1\\\\, \\\\mathrm{Vdc0}\\\\, \\\\mathrm{Zdcn}_{s}1\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{\\\\mathrm{Km}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Tpll}_{s}0\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{M1d} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1d} + \\\\mathrm{I1d}\\\\, \\\\mathrm{Kdq}\\\\, \\\\mathrm{i} + \\\\mathrm{I1q}\\\\, \\\\mathrm{Kdq} - \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1q}\\\\, \\\\mathrm{i} - \\\\mathrm{M1q}\\\\, \\\\mathrm{i}\\\\right)}{2} & 0 & -\\\\frac{\\\\mathrm{Km}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Tpll}_{s}0\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{M1d} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1d} + \\\\mathrm{I1d}\\\\, \\\\mathrm{Kdq}\\\\, \\\\mathrm{i} + \\\\mathrm{I1q}\\\\, \\\\mathrm{Kdq} - \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1q}\\\\, \\\\mathrm{i} - \\\\mathrm{M1q}\\\\, \\\\mathrm{i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{\\\\mathrm{Km}_{s}1\\\\, \\\\mathrm{Tpll}_{s}0\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{M1d} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1d} - \\\\mathrm{I1d}\\\\, \\\\mathrm{Kdq}\\\\, \\\\mathrm{i} + \\\\mathrm{I1q}\\\\, \\\\mathrm{Kdq} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1q}\\\\, \\\\mathrm{i} + \\\\mathrm{M1q}\\\\, \\\\mathrm{i}\\\\right)}{2} & 0 & \\\\frac{\\\\mathrm{Km}_{s}1\\\\, \\\\mathrm{Tpll}_{s}0\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{M1d} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1d} - \\\\mathrm{I1d}\\\\, \\\\mathrm{Kdq}\\\\, \\\\mathrm{i} + \\\\mathrm{I1q}\\\\, \\\\mathrm{Kdq} + \\\\mathrm{Hi}_{s}0\\\\, \\\\mathrm{I1q}\\\\, \\\\mathrm{i} + \\\\mathrm{M1q}\\\\, \\\\mathrm{i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - \\\\mathrm{Km}_{s}\\\\mathrm{n1}\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{Hi}_{s}0 + \\\\mathrm{Kdq}\\\\, \\\\mathrm{i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - \\\\mathrm{Km}_{s}1\\\\, \\\\mathrm{Vdc0}\\\\, \\\\left(\\\\mathrm{Hi}_{s}0 - \\\\mathrm{Kdq}\\\\, \\\\mathrm{i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eqn = fp.readlines()\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{d}{isp1}_{1} - {L1}_{s}{n1} + {Ci}_{d}{isp2}_{1}\\\\, F_{d}{isp1}_{2} & 0 & {Ci}_{d}{isp2}_{3}\\\\, F_{d}{isp1}_{2}\\\\\\\\ 0 & H_{d}{isp2}_{2} - {L1}_{s}0 & 0\\\\\\\\ {Ci}_{d}{isp2}_{1}\\\\, F_{d}{isp3}_{2} & 0 & H_{d}{isp3}_{3} - {L1}_{s}1 + {Ci}_{d}{isp2}_{3}\\\\, F_{d}{isp3}_{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {Kf}_{s}{n1} - S_{d}{isp1}_{1} - {Cv}_{d}{isp2}_{1}\\\\, F_{d}{isp1}_{2} & 0 &  - S_{d}{isp1}_{3} - {Cv}_{d}{isp2}_{3}\\\\, F_{d}{isp1}_{2}\\\\\\\\ 0 & {Kf}_{s}0 & 0\\\\\\\\  - S_{d}{isp3}_{1} - {Cv}_{d}{isp2}_{1}\\\\, F_{d}{isp3}_{2} & 0 & {Kf}_{s}1 - S_{d}{isp3}_{3} - {Cv}_{d}{isp2}_{3}\\\\, F_{d}{isp3}_{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {Km}_{s}{n1}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0\\\\\\\\ {Km}_{s}0\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s}{n1}\\\\, {Hv}_{s}{n1}\\\\, {Vdc0}}{2}\\\\right) & 0 & {Km}_{s}0\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}1\\\\, {Hv}_{s}1\\\\, {Vdc0}}{2}\\\\right)\\\\\\\\ 0 & {Km}_{s}1\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I1a}\\\\, {Kf}_{s}{n1}\\\\, {Zdcn}_{s}{n1}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s}{n1} + 2\\\\, {Cdc}_{s}{n1}\\\\, {Vdc0}\\\\, {Zdcn}_{s}{n1}} & 0 & -\\\\frac{3\\\\, {Kf}_{s}1\\\\, {Zdcn}_{s}1\\\\, \\\\overline{{I1a}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s}1 + 2\\\\, {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Zdcn}_{s}{n1}\\\\, \\\\left({I1a}\\\\, {L1}_{s}{n1} + {I1a}\\\\, {L1st} + {Kfst}\\\\, {U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s}{n1} + {Cdc}_{s}{n1}\\\\, {Vdc0}\\\\, {Zdcn}_{s}{n1}\\\\right)} & 0 & -\\\\frac{3\\\\, {Zdcn}_{s}1\\\\, \\\\left({L1}_{s}1\\\\, \\\\overline{{I1a}} + {L1st}\\\\, \\\\overline{{I1a}} + {Kfst}\\\\, \\\\overline{{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s}1 + {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{Km}_{s}{n1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{Km}_{s}{n1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {Km}_{s}{n1}\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 + {Kdq}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {Km}_{s}1\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 - {Kdq}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(eqn)):\n",
    "    eqn[i] = eqn[i].replace('\\\\mathrm','')\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rep_pattern_1(string_lst):\n",
    "    ret_lst = [];\n",
    "    for string in string_lst:\n",
    "        string = re.sub('\\{d\\}\\{isp','',string)\n",
    "        string = re.sub('\\}_\\{','',string)\n",
    "        string = '{'+string\n",
    "        ret_lst.append(string)\n",
    "    return ret_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{11} - {L1}_{s}{n1} + {Ci}_{21}\\\\, F_{12} & 0 & {Ci}_{23}\\\\, F_{12}\\\\\\\\ 0 & H_{22} - {L1}_{s}0 & 0\\\\\\\\ {Ci}_{21}\\\\, F_{32} & 0 & H_{33} - {L1}_{s}1 + {Ci}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {Kf}_{s}{n1} - S_{11} - {Cv}_{21}\\\\, F_{12} & 0 &  - S_{13} - {Cv}_{23}\\\\, F_{12}\\\\\\\\ 0 & {Kf}_{s}0 & 0\\\\\\\\  - S_{31} - {Cv}_{21}\\\\, F_{32} & 0 & {Kf}_{s}1 - S_{33} - {Cv}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {Km}_{s}{n1}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0\\\\\\\\ {Km}_{s}0\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s}{n1}\\\\, {Hv}_{s}{n1}\\\\, {Vdc0}}{2}\\\\right) & 0 & {Km}_{s}0\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}1\\\\, {Hv}_{s}1\\\\, {Vdc0}}{2}\\\\right)\\\\\\\\ 0 & {Km}_{s}1\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I1a}\\\\, {Kf}_{s}{n1}\\\\, {Zdcn}_{s}{n1}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s}{n1} + 2\\\\, {Cdc}_{s}{n1}\\\\, {Vdc0}\\\\, {Zdcn}_{s}{n1}} & 0 & -\\\\frac{3\\\\, {Kf}_{s}1\\\\, {Zdcn}_{s}1\\\\, \\\\overline{{I1a}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s}1 + 2\\\\, {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Zdcn}_{s}{n1}\\\\, \\\\left({I1a}\\\\, {L1}_{s}{n1} + {I1a}\\\\, {L1st} + {Kfst}\\\\, {U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s}{n1} + {Cdc}_{s}{n1}\\\\, {Vdc0}\\\\, {Zdcn}_{s}{n1}\\\\right)} & 0 & -\\\\frac{3\\\\, {Zdcn}_{s}1\\\\, \\\\left({L1}_{s}1\\\\, \\\\overline{{I1a}} + {L1st}\\\\, \\\\overline{{I1a}} + {Kfst}\\\\, \\\\overline{{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s}1 + {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{Km}_{s}{n1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{Km}_{s}{n1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {Km}_{s}{n1}\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 + {Kdq}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {Km}_{s}1\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 - {Kdq}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k in range(len(eqn)):\n",
    "    m = re.findall('\\{d\\}\\{isp[0-9]\\}_\\{[0-9]\\}',eqn[k])\n",
    "    mnew = rep_pattern_1(m)\n",
    "    for i in range(len(m)):\n",
    "        eqn[k] = eqn[k].replace(m[i],mnew[i])       \n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rep_pattern_2(string_lst):\n",
    "    ret_lst = [];\n",
    "    for string in string_lst:\n",
    "        string = re.sub('\\}\\{','',string)\n",
    "        ret_lst.append(string)\n",
    "    return ret_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{11} - {L1}_{sn1} + {Ci}_{21}\\\\, F_{12} & 0 & {Ci}_{23}\\\\, F_{12}\\\\\\\\ 0 & H_{22} - {L1}_{s}0 & 0\\\\\\\\ {Ci}_{21}\\\\, F_{32} & 0 & H_{33} - {L1}_{s}1 + {Ci}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {Kf}_{sn1} - S_{11} - {Cv}_{21}\\\\, F_{12} & 0 &  - S_{13} - {Cv}_{23}\\\\, F_{12}\\\\\\\\ 0 & {Kf}_{s}0 & 0\\\\\\\\  - S_{31} - {Cv}_{21}\\\\, F_{32} & 0 & {Kf}_{s}1 - S_{33} - {Cv}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {Km}_{sn1}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0\\\\\\\\ {Km}_{s}0\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{sn1}\\\\, {Hv}_{sn1}\\\\, {Vdc0}}{2}\\\\right) & 0 & {Km}_{s}0\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s}1\\\\, {Hv}_{s}1\\\\, {Vdc0}}{2}\\\\right)\\\\\\\\ 0 & {Km}_{s}1\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s}0\\\\, {Hv}_{s}0\\\\, {Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I1a}\\\\, {Kf}_{sn1}\\\\, {Zdcn}_{sn1}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{sn1} + 2\\\\, {Cdc}_{sn1}\\\\, {Vdc0}\\\\, {Zdcn}_{sn1}} & 0 & -\\\\frac{3\\\\, {Kf}_{s}1\\\\, {Zdcn}_{s}1\\\\, \\\\overline{{I1a}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s}1 + 2\\\\, {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Zdcn}_{sn1}\\\\, \\\\left({I1a}\\\\, {L1}_{sn1} + {I1a}\\\\, {L1st} + {Kfst}\\\\, {U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{sn1} + {Cdc}_{sn1}\\\\, {Vdc0}\\\\, {Zdcn}_{sn1}\\\\right)} & 0 & -\\\\frac{3\\\\, {Zdcn}_{s}1\\\\, \\\\left({L1}_{s}1\\\\, \\\\overline{{I1a}} + {L1st}\\\\, \\\\overline{{I1a}} + {Kfst}\\\\, \\\\overline{{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s}1 + {Cdc}_{s}1\\\\, {Vdc0}\\\\, {Zdcn}_{s}1\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{Km}_{sn1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{Km}_{sn1}\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s}0\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{Km}_{s}1\\\\, {Tpll}_{s}0\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s}0\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s}0\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {Km}_{sn1}\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 + {Kdq}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {Km}_{s}1\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s}0 - {Kdq}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k in range(len(eqn)):\n",
    "    m = re.findall('\\{s\\}\\{n[0-9]\\}',eqn[k])\n",
    "    mnew = rep_pattern_2(m)\n",
    "    for i in range(len(m)):\n",
    "        eqn[k] = eqn[k].replace(m[i],mnew[i])\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def rep_pattern_3(string_lst):\n",
    "    ret_lst = [];\n",
    "    for string in string_lst:\n",
    "        string = re.sub('\\}','',string)\n",
    "        string = string+'}'\n",
    "        ret_lst.append(string)\n",
    "    return ret_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{11} - {L1}_{sn1} + {Ci}_{21}\\\\, F_{12} & 0 & {Ci}_{23}\\\\, F_{12}\\\\\\\\ 0 & H_{22} - {L1}_{s0} & 0\\\\\\\\ {Ci}_{21}\\\\, F_{32} & 0 & H_{33} - {L1}_{s1} + {Ci}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {Kf}_{sn1} - S_{11} - {Cv}_{21}\\\\, F_{12} & 0 &  - S_{13} - {Cv}_{23}\\\\, F_{12}\\\\\\\\ 0 & {Kf}_{s0} & 0\\\\\\\\  - S_{31} - {Cv}_{21}\\\\, F_{32} & 0 & {Kf}_{s1} - S_{33} - {Cv}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {Km}_{sn1}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s0}\\\\, {Hv}_{s0}\\\\, {Vdc0}}{2}\\\\right) & 0\\\\\\\\ {Km}_{s0}\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{sn1}\\\\, {Hv}_{sn1}\\\\, {Vdc0}}{2}\\\\right) & 0 & {Km}_{s0}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{Hi}_{s1}\\\\, {Hv}_{s1}\\\\, {Vdc0}}{2}\\\\right)\\\\\\\\ 0 & {Km}_{s1}\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{Hi}_{s0}\\\\, {Hv}_{s0}\\\\, {Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I1a}\\\\, {Kf}_{sn1}\\\\, {Zdcn}_{sn1}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{sn1} + 2\\\\, {Cdc}_{sn1}\\\\, {Vdc0}\\\\, {Zdcn}_{sn1}} & 0 & -\\\\frac{3\\\\, {Kf}_{s1}\\\\, {Zdcn}_{s1}\\\\, \\\\overline{{I1a}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Zdcn}_{s1} + 2\\\\, {Cdc}_{s1}\\\\, {Vdc0}\\\\, {Zdcn}_{s1}}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Zdcn}_{sn1}\\\\, \\\\left({I1a}\\\\, {L1}_{sn1} + {I1a}\\\\, {L1st} + {Kfst}\\\\, {U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{sn1} + {Cdc}_{sn1}\\\\, {Vdc0}\\\\, {Zdcn}_{sn1}\\\\right)} & 0 & -\\\\frac{3\\\\, {Zdcn}_{s1}\\\\, \\\\left({L1}_{s1}\\\\, \\\\overline{{I1a}} + {L1st}\\\\, \\\\overline{{I1a}} + {Kfst}\\\\, \\\\overline{{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Zdcn}_{s1} + {Cdc}_{s1}\\\\, {Vdc0}\\\\, {Zdcn}_{s1}\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{Km}_{sn1}\\\\, {Tpll}_{s0}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s0}\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s0}\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{Km}_{sn1}\\\\, {Tpll}_{s0}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s0}\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {Hi}_{s0}\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{Km}_{s1}\\\\, {Tpll}_{s0}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s0}\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s0}\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{Km}_{s1}\\\\, {Tpll}_{s0}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {Hi}_{s0}\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {Hi}_{s0}\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {Km}_{sn1}\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s0} + {Kdq}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {Km}_{s1}\\\\, {Vdc0}\\\\, \\\\left({Hi}_{s0} - {Kdq}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k in range(len(eqn)):\n",
    "    m = re.findall('\\{s\\}[0-9]',eqn[k])\n",
    "    mnew = rep_pattern_3(m)\n",
    "    for i in range(len(m)):\n",
    "        eqn[k] = eqn[k].replace(m[i],mnew[i])\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rep_pattern_4(string_lst):\n",
    "    ret_lst = [];\n",
    "    for string in string_lst:\n",
    "        string = re.sub('\\}_\\{',',',string)\n",
    "        string = '_{'+string+'}'\n",
    "        ret_lst.append(string)\n",
    "    return ret_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{11} - {L_{1,sn1}} + {Ci}_{21}\\\\, F_{12} & 0 & {Ci}_{23}\\\\, F_{12}\\\\\\\\ 0 & H_{22} - {L_{1,s0}} & 0\\\\\\\\ {Ci}_{21}\\\\, F_{32} & 0 & H_{33} - {L_{1,s1}} + {Ci}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {K_{f,sn1}} - S_{11} - {Cv}_{21}\\\\, F_{12} & 0 &  - S_{13} - {Cv}_{23}\\\\, F_{12}\\\\\\\\ 0 & {K_{f,s0}} & 0\\\\\\\\  - S_{31} - {Cv}_{21}\\\\, F_{32} & 0 & {K_{f,s1}} - S_{33} - {Cv}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {K_{m,sn1}}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{H_{i,s0}}\\\\, {H_{v,s0}}\\\\, {Vdc0}}{2}\\\\right) & 0\\\\\\\\ {K_{m,s0}}\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{H_{i,sn1}}\\\\, {H_{v,sn1}}\\\\, {Vdc0}}{2}\\\\right) & 0 & {K_{m,s0}}\\\\, \\\\left(\\\\frac{\\\\overline{{M1a}}}{2} + \\\\frac{{H_{i,s1}}\\\\, {H_{v,s1}}\\\\, {Vdc0}}{2}\\\\right)\\\\\\\\ 0 & {K_{m,s1}}\\\\, \\\\left(\\\\frac{{M1a}}{2} + \\\\frac{{H_{i,s0}}\\\\, {H_{v,s0}}\\\\, {Vdc0}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I1a}\\\\, {K_{f,sn1}}\\\\, {Z_{dcn,sn1}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Z_{dcn,sn1}} + 2\\\\, {C_{dc,sn1}}\\\\, {Vdc0}\\\\, {Z_{dcn,sn1}}} & 0 & -\\\\frac{3\\\\, {K_{f,s1}}\\\\, {Z_{dcn,s1}}\\\\, \\\\overline{{I1a}}}{4\\\\, {Vdc0} - 2\\\\, {Iload}\\\\, {Z_{dcn,s1}} + 2\\\\, {C_{dc,s1}}\\\\, {Vdc0}\\\\, {Z_{dcn,s1}}}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Z_{dcn,sn1}}\\\\, \\\\left({I1a}\\\\, {L_{1,sn1}} + {I1a}\\\\, {L1st} + {Kfst}\\\\, {U1a}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Z_{dcn,sn1}} + {C_{dc,sn1}}\\\\, {Vdc0}\\\\, {Z_{dcn,sn1}}\\\\right)} & 0 & -\\\\frac{3\\\\, {Z_{dcn,s1}}\\\\, \\\\left({L_{1,s1}}\\\\, \\\\overline{{I1a}} + {L1st}\\\\, \\\\overline{{I1a}} + {Kfst}\\\\, \\\\overline{{U1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {Vdc0} - {Iload}\\\\, {Z_{dcn,s1}} + {C_{dc,s1}}\\\\, {Vdc0}\\\\, {Z_{dcn,s1}}\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{K_{m,sn1}}\\\\, {T_{pll,s0}}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {H_{i,s0}}\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {H_{i,s0}}\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{K_{m,sn1}}\\\\, {T_{pll,s0}}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {H_{i,s0}}\\\\, {I1d} + {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} - {H_{i,s0}}\\\\, {I1q}\\\\, {i} - {M1q}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{K_{m,s1}}\\\\, {T_{pll,s0}}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {H_{i,s0}}\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {H_{i,s0}}\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{K_{m,s1}}\\\\, {T_{pll,s0}}\\\\, {Vdc0}\\\\, \\\\left({M1d} + {H_{i,s0}}\\\\, {I1d} - {I1d}\\\\, {Kdq}\\\\, {i} + {I1q}\\\\, {Kdq} + {H_{i,s0}}\\\\, {I1q}\\\\, {i} + {M1q}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {K_{m,sn1}}\\\\, {Vdc0}\\\\, \\\\left({H_{i,s0}} + {Kdq}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {K_{m,s1}}\\\\, {Vdc0}\\\\, \\\\left({H_{i,s0}} - {Kdq}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k in range(len(eqn)):\n",
    "    m = re.findall('[a-z,0-9]*\\}_\\{sn*[0-9]\\}',eqn[k])\n",
    "    mnew = rep_pattern_4(m)\n",
    "    for i in range(len(m)):\n",
    "        eqn[k] = eqn[k].replace(m[i],mnew[i])\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def rep_pattern_5(string_lst):\n",
    "    ret_lst = [];\n",
    "    for string in string_lst:\n",
    "        string = string[0]+'_{'+string[1:]+'}'\n",
    "        ret_lst.append(string)\n",
    "    return ret_lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['\\\\left(\\\\begin{array}{ccc} H_{11} - {L_{1,sn1}} + {C_{i}}_{21}\\\\, F_{12} & 0 & {C_{i}}_{23}\\\\, F_{12}\\\\\\\\ 0 & H_{22} - {L_{1,s0}} & 0\\\\\\\\ {C_{i}}_{21}\\\\, F_{32} & 0 & H_{33} - {L_{1,s1}} + {C_{i}}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} {K_{f,sn1}} - S_{11} - {C_{v}}_{21}\\\\, F_{12} & 0 &  - S_{13} - {C_{v}}_{23}\\\\, F_{12}\\\\\\\\ 0 & {K_{f,s0}} & 0\\\\\\\\  - S_{31} - {C_{v}}_{21}\\\\, F_{32} & 0 & {K_{f,s1}} - S_{33} - {C_{v}}_{23}\\\\, F_{32} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & {K_{m,sn1}}\\\\, \\\\left(\\\\frac{\\\\overline{{M_{1a}}}}{2} + \\\\frac{{H_{i,s0}}\\\\, {H_{v,s0}}\\\\, {V_{dc0}}}{2}\\\\right) & 0\\\\\\\\ {K_{m,s0}}\\\\, \\\\left(\\\\frac{{M_{1a}}}{2} + \\\\frac{{H_{i,sn1}}\\\\, {H_{v,sn1}}\\\\, {V_{dc0}}}{2}\\\\right) & 0 & {K_{m,s0}}\\\\, \\\\left(\\\\frac{\\\\overline{{M_{1a}}}}{2} + \\\\frac{{H_{i,s1}}\\\\, {H_{v,s1}}\\\\, {V_{dc0}}}{2}\\\\right)\\\\\\\\ 0 & {K_{m,s1}}\\\\, \\\\left(\\\\frac{{M_{1a}}}{2} + \\\\frac{{H_{i,s0}}\\\\, {H_{v,s0}}\\\\, {V_{dc0}}}{2}\\\\right) & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {I_{1a}}\\\\, {K_{f,sn1}}\\\\, {Z_{dcn,sn1}}}{4\\\\, {V_{dc0}} - 2\\\\, {I_{load}}\\\\, {Z_{dcn,sn1}} + 2\\\\, {C_{dc,sn1}}\\\\, {V_{dc0}}\\\\, {Z_{dcn,sn1}}} & 0 & -\\\\frac{3\\\\, {K_{f,s1}}\\\\, {Z_{dcn,s1}}\\\\, \\\\overline{{I_{1a}}}}{4\\\\, {V_{dc0}} - 2\\\\, {I_{load}}\\\\, {Z_{dcn,s1}} + 2\\\\, {C_{dc,s1}}\\\\, {V_{dc0}}\\\\, {Z_{dcn,s1}}}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} 0 & 0 & 0\\\\\\\\ -\\\\frac{3\\\\, {Z_{dcn,sn1}}\\\\, \\\\left({I_{1a}}\\\\, {L_{1,sn1}} + {I_{1a}}\\\\, {L_{1st}} + {K_{fst}}\\\\, {U_{1a}}\\\\right)}{2\\\\, \\\\left(2\\\\, {V_{dc0}} - {I_{load}}\\\\, {Z_{dcn,sn1}} + {C_{dc,sn1}}\\\\, {V_{dc0}}\\\\, {Z_{dcn,sn1}}\\\\right)} & 0 & -\\\\frac{3\\\\, {Z_{dcn,s1}}\\\\, \\\\left({L_{1,s1}}\\\\, \\\\overline{{I_{1a}}} + {L_{1st}}\\\\, \\\\overline{{I_{1a}}} + {K_{fst}}\\\\, \\\\overline{{U_{1a}}}\\\\right)}{2\\\\, \\\\left(2\\\\, {V_{dc0}} - {I_{load}}\\\\, {Z_{dcn,s1}} + {C_{dc,s1}}\\\\, {V_{dc0}}\\\\, {Z_{dcn,s1}}\\\\right)}\\\\\\\\ 0 & 0 & 0 \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} \\\\frac{{K_{m,sn1}}\\\\, {T_{pll,s0}}\\\\, {V_{dc0}}\\\\, \\\\left({M_{1d}} + {H_{i,s0}}\\\\, {I_{1d}} + {I_{1d}}\\\\, {K_{dq}}\\\\, {i} + {I_{1q}}\\\\, {K_{dq}} - {H_{i,s0}}\\\\, {I_{1q}}\\\\, {i} - {M_{1q}}\\\\, {i}\\\\right)}{2} & 0 & -\\\\frac{{K_{m,sn1}}\\\\, {T_{pll,s0}}\\\\, {V_{dc0}}\\\\, \\\\left({M_{1d}} + {H_{i,s0}}\\\\, {I_{1d}} + {I_{1d}}\\\\, {K_{dq}}\\\\, {i} + {I_{1q}}\\\\, {K_{dq}} - {H_{i,s0}}\\\\, {I_{1q}}\\\\, {i} - {M_{1q}}\\\\, {i}\\\\right)}{2}\\\\\\\\ 0 & 0 & 0\\\\\\\\ -\\\\frac{{K_{m,s1}}\\\\, {T_{pll,s0}}\\\\, {V_{dc0}}\\\\, \\\\left({M_{1d}} + {H_{i,s0}}\\\\, {I_{1d}} - {I_{1d}}\\\\, {K_{dq}}\\\\, {i} + {I_{1q}}\\\\, {K_{dq}} + {H_{i,s0}}\\\\, {I_{1q}}\\\\, {i} + {M_{1q}}\\\\, {i}\\\\right)}{2} & 0 & \\\\frac{{K_{m,s1}}\\\\, {T_{pll,s0}}\\\\, {V_{dc0}}\\\\, \\\\left({M_{1d}} + {H_{i,s0}}\\\\, {I_{1d}} - {I_{1d}}\\\\, {K_{dq}}\\\\, {i} + {I_{1q}}\\\\, {K_{dq}} + {H_{i,s0}}\\\\, {I_{1q}}\\\\, {i} + {M_{1q}}\\\\, {i}\\\\right)}{2} \\\\end{array}\\\\right) \\n',\n",
       " '\\\\left(\\\\begin{array}{ccc} - {K_{m,sn1}}\\\\, {V_{dc0}}\\\\, \\\\left({H_{i,s0}} + {K_{dq}}\\\\, {i}\\\\right) & 0 & 0\\\\\\\\ 0 & 0 & 0\\\\\\\\ 0 & 0 & - {K_{m,s1}}\\\\, {V_{dc0}}\\\\, \\\\left({H_{i,s0}} - {K_{dq}}\\\\, {i}\\\\right) \\\\end{array}\\\\right) \\n']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for k in range(len(eqn)):\n",
    "    m = re.findall('[A-Z][a-z,0-9]*?\\}',eqn[k])\n",
    "    mnew = rep_pattern_5(m)\n",
    "    for i in range(len(m)):\n",
    "        eqn[k] = eqn[k].replace(m[i],mnew[i])\n",
    "    #eqn = eqn.replace('}}_{','')\n",
    "eqn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fp = open('Ytex.txt','w')\n",
    "fp.writelines(eqn)\n",
    "fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
